package cn.hlife.com.hlfhaemocyte.systemsetting.view;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.List;

import cn.hlife.com.hlfhaemocyte.MainActivity;
import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.utils.FragmentUtils;
import cn.hlife.com.hlfhaemocyte.utils.LogUtils;

/**
 * Created by My on 2017/7/9.
 */

public class CoefficientSetting  extends Fragment {
    Fragment  fragment;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.coefficient_setting, container, false);
        view.findViewById(R.id.fragment_content2);
//        fragment= getFragmentManager().findFragmentById(R.id.fragment_content2);
        RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.rdiogroup);
        ((MainActivity)getActivity()).middleText.setText("系数设置");
        ((MainActivity)getActivity()).rightText.setVisibility(View.VISIBLE);
        ((MainActivity)getActivity()).rightText.setText("保存");
        ((MainActivity)getActivity()).returnImageView.setVisibility(View.VISIBLE);
        ((MainActivity)getActivity()).rightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Fragment>   listFragment=getChildFragmentManager().getFragments();
                for (Fragment  fragment:listFragment ){
                    if(fragment instanceof  FullBooldFragment){
                        ((FullBooldFragment)fragment).checkInputIntegrity();
                    }else  if(fragment instanceof  FullBooldFragment){
                        ((DiluentFragment)fragment).checkInputIntegrity();
                    }
                }
                Toast.makeText(getContext(),"数据已保存",Toast.LENGTH_SHORT).show();
                ((MainActivity)getActivity()).currentFragment = FragmentUtils.replaceFragment
                        (getFragmentManager(), R.id.fragment_content, SettingFragment.class, null,false);
            }
        });
        ((MainActivity)getActivity()).returnImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                   ((MainActivity)getActivity()).currentFragment = FragmentUtils.replaceFragment
                           (getFragmentManager(), R.id.fragment_content, SettingFragment.class, null,false);

            }
        });
        radioGroup.check(R.id.full_blood);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                        switchFragment(checkedId);
            }
        });
        LogUtils.e("测试");
        fragment=FragmentUtils.switchFragment(getChildFragmentManager(), R.id.fragment_content2,null, FullBooldFragment.class,null);
        return view;

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        ((MainActivity)getActivity()).returnImageView.setVisibility(View.VISIBLE);
    }

    public  void  switchFragment(int checkedId){
        switch (checkedId) {
            case R.id.full_blood:
                fragment=FragmentUtils.switchFragment
                        (getChildFragmentManager(), R.id.fragment_content2,fragment, FullBooldFragment.class, null,false);
                break;
            case R.id.diluent:
                fragment=FragmentUtils.switchFragment
                        (getChildFragmentManager(), R.id.fragment_content2, fragment,DiluentFragment.class, null,false);
                break;

        }
    }

}
